package com.security.activity;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SignVM extends AndroidViewModel {
    public TextWatcher accountWatcher=new TextWatcher(){
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            account.setValue(s.toString());
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    public TextWatcher passwordWatcher=new TextWatcher(){
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            password.setValue(s.toString());
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    public TextWatcher password2Watcher=new TextWatcher(){
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            password2.setValue(s.toString());
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    public TextWatcher emailWatcher=new TextWatcher(){
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            email.setValue(s.toString());
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    public TextWatcher mobileWatcher=new TextWatcher(){
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            mobile.setValue(s.toString());
        }
        @Override
        public void afterTextChanged(Editable s) {
        }
    };
    @SuppressLint("StaticFieldLeak")
    Context context;
    public  MutableLiveData<String> account,password,password2,email,mobile,statue;
    public SignVM(@NonNull Application application) {
        super(application);
        context=application;
        if (account==null){ account=new MutableLiveData<>(); }
        if (password==null){ password=new MutableLiveData<>(); }
        if (password2==null){ password2=new MutableLiveData<>(); }
        if (email==null){ email=new MutableLiveData<>(); }
        if (mobile==null){ mobile=new MutableLiveData<>(); }
        if (statue==null){ statue=new MutableLiveData<>(); }
    }
    public void sign(View view) {
        if (account.getValue()==null || password.getValue()==null || password2.getValue()==null||email.getValue()==null||mobile.getValue()==null){
            statue.setValue("请输入完整");
        }else{
            if(!password2.getValue().equals(password.getValue())){
                statue.setValue("两次密码不一样");
            }else{
                MediaType JSON = MediaType.parse("application/json;charset=utf-8");
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("username",account.getValue());
                    jsonObject.put("password",password.getValue());
                    jsonObject.put("email",email.getValue());
                    jsonObject.put("mobile",mobile.getValue());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                RequestBody requestBody = RequestBody.create(JSON, String.valueOf(jsonObject));
                Request.Builder request = new Request.Builder().url("https://api2.bmob.cn/1/users");
                request.addHeader("X-Bmob-Application-Id","a0e84e21a8ae973db9c1388a990533f8");
                request.addHeader("X-Bmob-REST-API-Key","c97d9ff0b1c23e4e5749b04d963ab041");
                request.addHeader("Content-Type","application/json");
                request.post(requestBody);
                final OkHttpClient client=new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS).connectTimeout(10,TimeUnit.SECONDS).writeTimeout(60,TimeUnit.SECONDS).build();
                final Call call=client.newCall(request.build());
                Log.i("emmmmm","2345678");
                new Thread(() -> {
                    try {
                        Response response=call.execute();
                        if (response.code()==201){
                            Looper.prepare();
                            statue.postValue("注册成功");
                            Looper.loop();
                        }else{
                            assert response.body() != null;
                            JSONObject jsonObject1=new JSONObject(response.body().string());
                            statue.postValue("注册失败，"+jsonObject1.getString("error"));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        }
    }
}
